/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.enterprise.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import lombok.SneakyThrows;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.enterprise.excel.AwardCorporateExcel;
import org.springblade.modules.enterprise.excel.AwardCorporateImportListener;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.excel.UserExcel;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.enterprise.entity.AwardCorporate;
import org.springblade.modules.enterprise.vo.AwardCorporateVO;
import org.springblade.modules.enterprise.service.IAwardCorporateService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *  控制器
 *
 * @author Blade
 * @since 2022-07-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/blade-enterprise/awardcorporate")
@Api(value = "", tags = "接口")
public class AwardCorporateController extends BladeController {

	private IAwardCorporateService awardCorporateService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入awardCorporate")
	public R<AwardCorporate> detail(AwardCorporate awardCorporate) {
		AwardCorporate detail = awardCorporateService.getOne(Condition.getQueryWrapper(awardCorporate));
		return R.data(detail);
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入awardCorporate")
	public R<IPage<AwardCorporate>> list(@RequestParam Map<String, Object> awardCorporate, Query query) {
		IPage<AwardCorporate> pages = awardCorporateService.page(Condition.getPage(query), Condition.getQueryWrapper(awardCorporate,AwardCorporate.class));
		return R.data(pages);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入awardCorporate")
	public R<IPage<AwardCorporateVO>> page(AwardCorporateVO awardCorporate, Query query) {
		IPage<AwardCorporateVO> pages = awardCorporateService.selectAwardCorporatePage(Condition.getPage(query), awardCorporate);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入awardCorporate")
	public R save(@Valid @RequestBody AwardCorporate awardCorporate) {
		return R.status(awardCorporateService.save(awardCorporate));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入awardCorporate")
	public R update(@Valid @RequestBody AwardCorporate awardCorporate) {
		return R.status(awardCorporateService.updateById(awardCorporate));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入awardCorporate")
	public R submit(@Valid @RequestBody AwardCorporate awardCorporate) {
		return R.status(awardCorporateService.saveOrUpdate(awardCorporate));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(awardCorporateService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导入获奖企业
	 */
	@PostMapping("import-award-corporate")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "导入获奖企业", notes = "传入excel")
	public R importUser(MultipartFile file, Integer isCovered) {
		String filename = file.getOriginalFilename();
		if (Func.isEmpty(filename)) {
			throw new RuntimeException("请上传文件!");
		}
		if ((!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx"))) {
			throw new RuntimeException("请上传正确的excel文件!");
		}
		InputStream inputStream;
		try {
			AwardCorporateImportListener importListener = new AwardCorporateImportListener(awardCorporateService);
			inputStream = new BufferedInputStream(file.getInputStream());
			ExcelReaderBuilder builder = EasyExcel.read(inputStream, AwardCorporateExcel.class, importListener);
			builder.doReadAll();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.success("操作成功");
	}
	/**
	 * 导出模板
	 */
	@SneakyThrows
	@GetMapping("export-template")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出模板")
	public void exportAwardCorporate(HttpServletResponse response) {
		List<AwardCorporateExcel> list = new ArrayList<>();
		list.add(AwardCorporateExcel.getExcelExample());
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding(StandardCharsets.UTF_8.name());
		String fileName = URLEncoder.encode("获奖企业数据模板", StandardCharsets.UTF_8.name());
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		EasyExcel.write(response.getOutputStream(), AwardCorporateExcel.class).sheet("获奖企业数据表").doWrite(list);
	}

	/**
	 * 导出获奖企业
	 */
	@SneakyThrows
	@GetMapping("export-award-corporate")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "导出获奖企业", notes = "传入awardCorporate")
	public void exportAwardCorporate(@ApiIgnore @RequestParam Map<String, Object> awardCorporate,HttpServletResponse response) {
		QueryWrapper<AwardCorporate> queryWrapper = Condition.getQueryWrapper(awardCorporate, AwardCorporate.class);
		queryWrapper.lambda().eq(AwardCorporate::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<AwardCorporateExcel> awardCorporateExcelList=new ArrayList<>();
		List<AwardCorporate> awardCorporateList = awardCorporateService.list(queryWrapper);
		awardCorporateList.forEach(awardCorporate1 -> {
			awardCorporateExcelList.add(Objects.requireNonNull(BeanUtil.copy(awardCorporate1,AwardCorporateExcel.class)));
		});
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding(StandardCharsets.UTF_8.name());
		String fileName = URLEncoder.encode("获奖企业数据导出", StandardCharsets.UTF_8.name());
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		EasyExcel.write(response.getOutputStream(), AwardCorporateExcel.class).sheet("获奖企业数据表").doWrite(awardCorporateExcelList);
	}
}
